/**
 * 
 */
package com.sign.signsystem;

import com.biz.service.UserService;
import com.core.entity.UserEntity;
import com.core.entity.UserEntity.Sex;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author Bosto
 * 
 */
public class UserDetailActivity extends HandlerActivity {
	EditText nameET, sexET, emailET;
	TextView noTextView;
	Button updateBtn, submitBtn;
	boolean updateStatus = false;
	int updateFlag = View.GONE;
	int UPDATE_FROM_UNPDATEBTN = 110;
	int UPDATE_FROM_SUBMITBTN = 220;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_detail);
		initComponent();
	}

	private void initComponent() {
		noTextView = (TextView) findViewById(R.id.loginIdTextView);
		nameET = (EditText) findViewById(R.id.name_edittext);
		sexET = (EditText) findViewById(R.id.sex_edittext);
		emailET = (EditText) findViewById(R.id.email_edittext);
		updateBtn = (Button) findViewById(R.id.update_btn);
		submitBtn = (Button) findViewById(R.id.submit_btn);
		UserEntity user = ((LocationApplication)getApplication()).getUserEntity();
		nameET.setText(user.getName());
		sexET.setText(user.getSex().equals(Sex.girl) ? R.string.women
				: R.string.man);
		emailET.setText(user.getEmail());
		noTextView.setText(user.getNo());
		updateButtonStatus(false);
		updateBtn.setOnClickListener(this);
		submitBtn.setOnClickListener(this);
	}

	private void updateButtonStatus(boolean change) {
		if (change) {
			updateStatus = !updateStatus;
			updateFlag = 8 - updateFlag;
		}
		submitBtn.setEnabled(updateStatus);
		submitBtn.setVisibility(updateFlag);
		nameET.setEnabled(updateStatus);
		sexET.setEnabled(updateStatus);
		emailET.setEnabled(updateStatus);
		updateBtn.setClickable(!updateStatus);
		updateBtn.setVisibility(8 - updateFlag);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_btn: {
			Message message = new Message();
			message.what = UPDATE_FROM_UNPDATEBTN;
			mHandler.sendMessage(message);
			break;
		}
		case R.id.submit_btn: {
			new Thread(new Runnable() {
				public void run() {
					Message message = new Message();
					if (new UserService().udpateUser()) {
						message.what = UPDATE_FROM_SUBMITBTN + 1;
					} else {
						message.what = UPDATE_FROM_SUBMITBTN + 2;
					}
					mHandler.sendMessage(message);
				}
			}).start();
		}
		default:
			break;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sign.signsystem.HandlerActivity#hanldeMessage(android.os.Message)
	 */
	@Override
	void hanldeMessage(Message msg) {
		switch (msg.what) {
		case 110:
			updateButtonStatus(true);
			break;
		case 221: {
			showLongMessage("更改用户信息成功");
			updateButtonStatus(true);
			break;
		}
		case 222: {
			showLongMessage(R.string.warning_message);
			updateButtonStatus(true);
			break;
		}
		default:
			break;
		}
	}
}
